package services;

import domain.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 22/08/12
 * Time: 12:33
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ProtocoloCompromisopaisService {

    private ProtocoloCompromisopaisDAO protocoloCompromisopaisDAO;
    private CompromisoPaisService compromisoPaisService;
    private ScorecardDAO scorecardDAO;

    public ProtocoloCompromisopaisService() {
    }

    @Autowired
    public ProtocoloCompromisopaisService(ProtocoloCompromisopaisDAO protocoloCompromisopaisDAO, CompromisoPaisService compromisoPaisService,ScorecardDAO scorecardDAO) {
        this.protocoloCompromisopaisDAO = protocoloCompromisopaisDAO;
        this.compromisoPaisService = compromisoPaisService;
        this.scorecardDAO = scorecardDAO;
    }

    public List<Compromisopais> findCompromisoPaisByProtocoloId(String protocoloid){
        return protocoloCompromisopaisDAO.findCompromisoPaisByProtocoloId(protocoloid);

    }

    public ProtocoloCompromisopais getByProtocoloIdCompromisopaisId(String protocoloId, Integer compromisopaisId){
        return protocoloCompromisopaisDAO.getByProtocoloIdCompromisopaisId(protocoloId,compromisopaisId);
    }

    public List<ProtocoloCompromisopais> findAll(){
        return protocoloCompromisopaisDAO.findAll();
    }

    public ProtocoloCompromisopais save(ProtocoloCompromisopais pcs) {
        pcs.setCompromisopais(compromisoPaisService.findById(pcs.getCompromisopais().getId()));
        pcs.setId(new ProtocoloCompromisopaisId(pcs.getProtocolo().getId(), pcs.getCompromisopais().getId()));
        return protocoloCompromisopaisDAO.merge(pcs);
    }

    public List<ProtocoloCompromisopais> getWithDataForCommitment(String protocoloID){
        return protocoloCompromisopaisDAO.getWithDataForCommitment(protocoloID);
    }

    public ProtocoloCompromisopais findById(ProtocoloCompromisopaisId id){
        return protocoloCompromisopaisDAO.findById(id);
    }

    public void attachDirty(ProtocoloCompromisopais protocoloCompromisopais){
        protocoloCompromisopaisDAO.attachDirty(protocoloCompromisopais);
    }

    public List<Compromisopais> getByProtocoloId(String protocoloid){
        return compromisoPaisService.findByProtocoloId(protocoloid);
    }

    public Compromisopais getCompromisopaisById(Integer compromisopaisId){
        return compromisoPaisService.findById(compromisopaisId);
    }
    public Compromisopais getCompromisopaisByCountry(String country){
        return compromisoPaisService.findByCountry(country);
    }

    public void saveScorecard(ProtocoloCompromisopais protocoloCompromisopais, Scorecard scorecard){

        if( protocoloCompromisopais.getScorecard() != null ){
            Scorecard scorecard1 = protocoloCompromisopais.getScorecard();

           //protocoloCompromisopais.setScorecard(null);
            //save(protocoloCompromisopais);
            scorecardDAO.deleteScorecardById(scorecard1.getId());;
        }

        for(Protocolscorecard protocolscorecard : scorecard.getProtocolscorecards()  ){
            if ( protocolscorecard.getMilestone().toUpperCase().contains("CHRONIC") )
                protocoloCompromisopais.setPlannedFpfv(protocolscorecard.getPlannedDate());
        }
        protocoloCompromisopais.setScorecard(scorecard);
        save(protocoloCompromisopais);
    }


    public List<ProtocoloCompromisopais> finByIds(Protocolo protocolo, List<Integer> paises) {
        List<ProtocoloCompromisopais> list = new ArrayList<ProtocoloCompromisopais>();
        for(Integer paisId : paises){
            ProtocoloCompromisopaisId pcpid = new ProtocoloCompromisopaisId();
            pcpid.setCompromisopaisid(paisId);
            pcpid.setProtocoloid(protocolo.getId());
            ProtocoloCompromisopais pcp = protocoloCompromisopaisDAO.findById(pcpid);
            if(pcp != null){
                list.add(pcp);
            }
        }

        return list;
    }
    public List<ScorecardReports> findScorecardForReports(Integer compromisopais, Integer therapeuticArea) {

        return protocoloCompromisopaisDAO.findScorecardForReports(compromisopais, therapeuticArea);

    }

    public List<Integer> findDurationById(Integer id) {

        return protocoloCompromisopaisDAO.findDurationById(id);

    }


}
